package com.czy.seed.mvc.itextPdf;

import com.czy.seed.mvc.home.entity.PurchaseDetailedList;
import com.czy.seed.mvc.home.entity.PurchaseOrderDetailVo;
import com.czy.seed.mvc.home.entity.ResultValidation;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class CreatePdf_Quotation {
    /**
     * @quote_unit 报价单位名称
     * @name 项目名称
     * @lastTime 截止日期
     * @list 采购清单列表
     * @createTime 创建时间
     * @cgr 采购人
     * @cgdw 采购单位
     * @cjjedaxie 成交大写金额
     * @cjjexx 成交小写
     * @frdb 法人
     * @lsTime 签订时间（可空）
     */
    public void createPdf(String url,String path, String quote_unit, String name, String lastTime, List<PurchaseOrderDetailVo> list, String cgr, String cgdw, String createTime, String cjjedaxie, String cjjexx, String frdb, String lsTime) throws FileNotFoundException, DocumentException, IOException {

        File file = new File(url);
        if (!file.exists()&& !file .isDirectory()) {
            file.mkdirs();
        }
        Float f = 25f;
        // 创建文件
        Document document = new Document();
        document.setPageSize(PageSize.A4);
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
        //打开文件
        document.open();
        try {
            //中文字体,解决中文不能显示问题
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font fontChar = new Font();
            fontChar.setSize(7);
            fontChar.setColor(BaseColor.DARK_GRAY);
            //黑色字体
            Font blueFont = new Font(bfChinese);
            blueFont.setColor(BaseColor.BLACK);
            //段落文本
            blueFont.setSize(20);
            Paragraph paragraphBlue = new Paragraph("张掖市市级政府采购协议供货和定点采购", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("二次询价成交结果确认通知书", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraphBlue);
            blueFont.setSize(14);
            paragraphBlue = new Paragraph(quote_unit + "  ：", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("经过二次询价和评审，我单位采购的", blueFont);
            paragraphBlue.setIndentationLeft(16); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            Chunk tChunk2 = new Chunk("   " + name + "   ", blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            Chunk tChunk3 = new Chunk("项目，确认你单位为成交供应商，请于", blueFont);
            paragraphBlue.add(tChunk3);
            tChunk2 = new Chunk("   " + lastTime + "   ", blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            tChunk3 = new Chunk("前派人与我单位签订政府采购合同，特此通知。", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("采购内容（可加附页）：", blueFont);
            paragraphBlue.setIndentationLeft(4); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            //添加表格
            PdfPTable table = new PdfPTable(new float[]{50, 80, 80, 50, 50, 50, 80});// 8列的表格以及单元格的宽度。
//        table.seto// 顶部空白区高度
            table.setTotalWidth(440);//表格整体宽度
            PdfPCell cell;
            cell = new PdfPCell(new Phrase("采购清单列表(价格单位/元)", blueFont));
            cell.setColspan(7);//占据八列
            int si = 2 + list.size();
            cell.setRowspan(si);
            table.addCell(cell);
            table.addCell(new Paragraph("序号", blueFont));
            table.addCell(new Paragraph("品牌", blueFont));
            table.addCell(new Paragraph("型号", blueFont));
            table.addCell(new Paragraph("数量", blueFont));
            table.addCell(new Paragraph("单价", blueFont));
            table.addCell(new Paragraph("总价", blueFont));
            table.addCell(new Paragraph("购买方式", blueFont));
            int k = 1;
            for (PurchaseOrderDetailVo s : list) {//将集合内的对象循环写入到表格
                table.addCell(new Paragraph(k + "", blueFont));
                table.addCell(new Paragraph(String.valueOf(s.getBrand()), blueFont));
                table.addCell(new Paragraph(String.valueOf(s.getGgxh()), blueFont));
                table.addCell(new Paragraph(String.valueOf(s.getNum()), blueFont));
                table.addCell(new Paragraph(String.valueOf(s.getDj()), blueFont));
                table.addCell(new Paragraph(String.valueOf(s.getTotal()), blueFont));
                if("二次询价".equals(s.getGmfs())) {
                    table.addCell(new Paragraph(String.valueOf(s.getGmfs()), blueFont));
                }else{
                    table.addCell(new Paragraph("比价购买", blueFont));
                }
                k++;
            }
            document.add(table);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("采购人（盖章） ：", blueFont);
            paragraphBlue.setIndentationLeft(4); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk(cgr, blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("单位负责人签字或盖章：                         ", blueFont);
            paragraphBlue.setIndentationLeft(4); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk(cgdw, blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("", blueFont);
            paragraphBlue.setIndentationLeft(4); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_RIGHT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk(createTime, blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph(" 成交供应商（盖章）：", blueFont);
            paragraphBlue.setIndentationLeft(4); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk(quote_unit, blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("成交金额（大写）：", blueFont);
            paragraphBlue.setIndentationLeft(4); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk(cjjedaxie, blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("小写¥ ", blueFont);
            paragraphBlue.setIndentationLeft(248); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk(cjjexx, blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("法定代表人或委托代理人（签字或盖章）：         ", blueFont);
            paragraphBlue.setIndentationLeft(4); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk("      " + frdb + "      ", blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph(" ", blueFont);
            paragraphBlue.setIndentationLeft(248); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_RIGHT);
            paragraphBlue.setLeading(f);
            tChunk2 = new Chunk("      " + lsTime, blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            //关闭文档
            document.close();
            //关闭书写器
            writer.close();

            ZipCompress zipCom = new ZipCompress(path.replace(".pdf",".zip"),path);
            try
            {
                zipCom.zip();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }




        } catch (Exception e) {
            throw e;
        } finally {
            if (writer != null) {
                writer.close();
            }
            if (document != null) {
                document.close();
            }
        }
    }
}
